<?php
namespace LMH\RealEstateBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class IMSPropertyController extends Controller
{
    public function showAction()
    { 
        $request=$this->get('request')->request;
        
        //Get the property selected
        $propertyselected=$request->get('propertyselected');
        
        if ($request->get('btnew')) { 
            //New property
            return $this->forward('LMHRealEstateBundle:IMSPropertyNew:show');
        }
        else if ($request->get('btedit') and $propertyselected!=null) { 
            //Edit property
            return $this->forward('LMHRealEstateBundle:IMSPropertyEdit:show', array('id' => $propertyselected));
        }
        else if ($request->get('btdelete') and $propertyselected!=null) { 
            //Delete property  
            return $this->forward('LMHRealEstateBundle:IMSProperty:delete', array('id' => $propertyselected));
        }
        else if ($request->get('btcontract') and $propertyselected!=null) { 
            //Create a contract
            //Check if the property has a contract
            $emcontract = $this->getDoctrine()->getEntityManager();
            $contract = $emcontract->getRepository('LMHRealEstateBundle:Contract')->checkContractByProperty($propertyselected);
            if($contract==0)
            {
                return $this->forward('LMHRealEstateBundle:IMSPropertyContractNew:show', array('id' => $propertyselected));
            }
            else {
                return $this->forward('LMHRealEstateBundle:IMSPropertyContractEdit:show', array('id' => $propertyselected));
            }
        }
        else
        {
            //Show the properties
            //--------------------
            
            //Get active towns
	    $em1 = $this->getDoctrine()->getEntityManager();
	    $towns = $em1->getRepository('LMHRealEstateBundle:Town')->getAllActive();
	    //Get active property types
	    $em2 = $this->getDoctrine()->getEntityManager();
	    $types = $em2->getRepository('LMHRealEstateBundle:PropertyType')->getAllActive();
	    //Get active Property State
	    $em3 = $this->getDoctrine()->getEntityManager();
	    $states = $em3->getRepository('LMHRealEstateBundle:PropertyState')->getAllActive();
	    //Get active Property Status
	    $em4 = $this->getDoctrine()->getEntityManager();
	    $status = $em4->getRepository('LMHRealEstateBundle:PropertyStatus')->getAllActive();
	    $em5 = $this->getDoctrine()->getEntityManager();
	    $salesagents = $em5->getRepository('LMHRealEstateBundle:SalesAgent')->getAllActive();
	    //Get active Owners
	    $em6 = $this->getDoctrine()->getEntityManager();
	    $owners = $em6->getRepository('LMHRealEstateBundle:Owner')->getAllActive();    
            
            $request=$this->get('request')->request;
            $perpage=$request->get('perpage');
            $sort=$request->get('sort');
        
            if ($request->get('page'))
            {    
                $page=$request->get('page');
            }
            
            if($perpage=="" || $perpage==NULL)
            {
                $perpage=10;
            }
            if($sort=="" || $sort==NULL)
            {
                $sort=0;
            }
            
            if($page=="" || $page==NULL || $page==0 )
            {
                $page=1;
                $parameters=array(
                    'keywords' => "",
                    'idtown' => 0,
                    'idtype'  => 0,
                    'idsalesagent'  => 0,
                    'idstatus' => 0,
                    'idstate'  => 0,
                    'pricemax'  => 0,
                    'pricemin'  => 0,
                    'metresmax'  => 0,
                    'metresmin'  => 0,
                    'bedsmax'  => 0,
                    'bedsmin'  => 0,
                    'bathsmax'  => 0,
                    'bathsmin'  => 0,
                    'page'  => $page,
                    'perpage'  => 10,
                    'sort'  => 0,
                );
                $session = $this->getRequest()->getSession();
                $session->clear();
                $session->set('parameters', $parameters);
            }
            else
            {
                if ($request->get('page'))
                {
                    $page=$request->get('page');
                    $parameters=array(
                        'keywords' => $request->get('keywords'),
                        'idtown' => $request->get('idtown'),
                        'idtype'  => $request->get('idtype'),
                        'idstatus' => $request->get('idstatus'),
                        'idstate'  => $request->get('idstate'),
                        'idsalesagent'  => $request->get('idsalesagent'),
                        'pricemax'  => $request->get('pricemax'),
                        'pricemin'  => $request->get('pricemin'),
                        'metresmax'  => $request->get('metresmax'),
                        'metresmin'  => $request->get('metresmin'),
                        'bedsmax'  => $request->get('bedsmax'),
                        'bedsmin'  => $request->get('bedsmin'),
                        'bathsmax'  => $request->get('bathsmax'),
                        'bathsmin'  => $request->get('bathsmin'),
                        'page'  => $page,
                        'perpage'  => $perpage,
                        'sort'  => $sort,
                    );
                    $session = $this->getRequest()->getSession();
                    $session->clear();
                    $session->set('parameters', $parameters);
                }
            }
            
            $session= $this->getRequest()->getSession();
            $paramsession=$session->get('parameters');
            $keywords= $paramsession["keywords"];
            $idtown= $paramsession["idtown"];
            $idtype= $paramsession["idtype"];
            $idsalesagent= $paramsession["idsalesagent"];
            $idstatus= $paramsession["idstatus"];
            $idstate= $paramsession["idstate"];
            $pricemax= $paramsession["pricemax"];
            $pricemin= $paramsession["pricemin"];
            $metresmax= $paramsession["metresmax"];
            $metresmin= $paramsession["metresmin"];
            $bedsmax= $paramsession["bedsmax"];
            $bedsmin= $paramsession["bedsmin"];
            $bathsmax= $paramsession["bathsmax"];
            $bathsmin= $paramsession["bathsmin"];
            $perpage= $paramsession["perpage"];
            $sort= $paramsession["sort"];
            
            $parameters=array(
                'keywords' => $paramsession["keywords"],
                'idtown' => $paramsession["idtown"],
                'idtype'  => $paramsession["idtype"],
                'idsalesagent'  => $paramsession["idsalesagent"],
                'idstatus' => $paramsession["idstatus"],
                'idstate'  => $paramsession["idstate"],
                'pricemax'  => $paramsession["pricemax"],
                'pricemin'  => $paramsession["pricemin"],
                'metresmax'  => $paramsession["metresmax"],
                'metresmin'  => $paramsession["metresmin"],
                'bedsmax'  => $paramsession["bedsmax"],
                'bedsmin'  => $paramsession["bedsmin"],
                'bathsmax'  => $paramsession["bathsmax"],
                'bathsmin'  => $paramsession["bathsmin"],
                'perpage'  => $paramsession["perpage"],
                'page'  => $page,
                'sort'  => $paramsession["sort"],
            );
            $session->clear();
            $session->set('parameters', $parameters);

            $em = $this->getDoctrine()->getEntityManager();
            $query8 = $em->getRepository('LMHRealEstateBundle:Property')
                ->searchProperties("IMS",$keywords,NULL,$idsalesagent,$idstatus,$idstate,
                $idtype,$pricemin,$pricemax,$metresmin,$metresmax,$bedsmin,$bedsmax,
                $bathsmin,$bathsmax,NULL,$idtown,$sort,NULL,NULL);
            
            if($perpage=="" || $perpage==NULL)
            {
                $perpage=10;
            }
            
            $paginator  = $this->get('knp_paginator');
            $properties = $paginator->paginate(
                $query8,
                $page/*page number*/,
                $perpage/*limit per page*/,array('distinct' => false)
            );

            return $this->render('LMHRealEstateBundle:Backend:IMSProperty.html.twig', array(
                'properties' => $properties,
                'parameters' => $parameters,
                'towns' => $towns,
                'types' => $types, 
                'states' => $states,
                'status' => $status,
                'salesagents' => $salesagents,
                'owners' => $owners,
                ));
	}
    }
    
    //This action deletes a property from the IMS Property Management
    public function deleteAction($id)
    { 
        //Deleting the property by idproperty
        $em1 = $this->getDoctrine()->getEntityManager();
        $deleted = $em1->getRepository('LMHRealEstateBundle:Property')->deleteProperty($id);
        
        //If the property is deleted we show the IMS Property Management
        if($deleted!=0)
        {
            $request=$this->get('request')->request;
            $request->set('btdelete',"");
            return $this->redirect($this->generateUrl('imspropertymanagement'));
        }
        else //If the property cannot be deleted we show the error page
        {
            $messages="It is not possible to delete the property: It has a contract or is published on the webpage";
            $messages.= "<br/><br/><a href=ims-property-management>Return</a>";
            return $this->render('LMHRealEstateBundle:Backend:Error.html.twig', array(
            'messages' => $messages,
            ));
        }
    }
    
}